package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    //区域方法
    @Autowired
    private IRegionService regionService;
    //目的地方法
    @Autowired
    private IDestinationService destinationService;

    //查询所有区域
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery qo) {
        //分页查询方法，查出来的是区域对象
        Page<Region> page = regionService.query(qo);
        // 将分页数据放到模型中共享到前端页面
        model.addAttribute("page", page);
        return "region/list";
    }

    //查询所有目的地
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) {
        return destinationService.listAll();
    }

    //保存或更新
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        //保存或更新
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    //根据区域id查询关联目的地
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid) {
        //根据区域id查询关联目的地
        List<Destination> list = destinationService.getDestByRegionId(rid);
        return list;
    }

    //根据区域id 跟 热门状态 设置热门
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot, String id) {
        //根据区域id 跟 热门状态 设置热门
        regionService.changeHotValue(hot, id);
        return JsonResult.success();
    }

    //根据id删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        regionService.delete(id);
        return JsonResult.success();
    }
}
